﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using Entity;

namespace DAO
{
    public class QuanLyLoaiKHDAO
    {
        public static DataTable LayThongTinLoaiKH()
        {
            SqlConnection connect = null;
            DataTable rs = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }
                String cmdText = "SELECT * FROM LOAI_KHACH_HANG";
                rs = SqlHelper.ExecuteDataset(connect, CommandType.Text, cmdText).Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (rs != null)
                    ((IDisposable)rs).Dispose();

                if (connect != null)
                    connect.Dispose();
            }
            return rs;
        }

        public static void SuaLoaiKH(LoaiKhachHang loaikh)
        {
            SqlConnection connect = null;
            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] param = new SqlParameter[3];
                param[0] = new SqlParameter("@maloaikh", loaikh.maloaikhachhang);
                param[1] = new SqlParameter("@capdo", loaikh.capdo);
                param[2] = new SqlParameter("@giamgia", loaikh.giamgia);
                String cmdText = "UPDATE LOAI_KHACH_HANG SET CapDo=@capdo,GiamGia=@giamgia WHERE MaLoaiKhachHang=@maloaikh";
                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, param);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static void XoaLoaiKH(Int32 maloaikh)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@maloaikh", maloaikh);
                String cmdText = "DELETE LOAI_KHACH_HANG WHERE MaLoaiKhachHang = @maloaikh";
                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, par);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static void ThemLoaiKH(LoaiKhachHang loaikh)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@capdo", loaikh.capdo);
                par[1] = new SqlParameter("@giamgia", loaikh.giamgia);
                String cmdText = "INSERT INTO LOAI_KHACH_HANG VALUES (@capdo,@giamgia)";

                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, par);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }
    }
}
